{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f6b49f95",
   "metadata": {},
   "source": [
    "### dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "804591eb",
   "metadata": {},
   "source": [
    "python中内置了字典，其他语言中的map功能，使用键-值（key-value）存储，具有极快的查找速度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "58966ff7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "95"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'Michael':95, 'Bob':75, 'Tracy':85}\n",
    "d['Michael']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73a11f98",
   "metadata": {},
   "source": [
    "list的查找方法相当于从字典第一页开始翻，翻到要查找的目标为止。dict就是和常见查阅字典的方法一样，根据key的值，直接得到存放value的存放位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2e22e3a",
   "metadata": {},
   "source": [
    "除了初始化外，可以通过key放入value。另外一个key只对应一个value"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56d52667",
   "metadata": {},
   "source": [
    "如果key不再dict内，则会报错，可以通过in来判断key是否存在，也可以用dict提供的方法get，可以返回None，或自己指定的value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d5b8c38c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Thomas' in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2774caa0",
   "metadata": {},
   "outputs": [],
   "source": [
    "d.get('Thomas')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "48b54375",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.get('Thomas', -1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb9aee1b",
   "metadata": {},
   "source": [
    "要删除一个key，用pop(key)方法，对应的value也会从dict中删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3d02eb0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Michael': 95, 'Tracy': 85}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.pop('Bob')\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1987f22",
   "metadata": {},
   "source": [
    "请注意，dict内部存放的顺序和key放入的顺序是没关系的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a454eba3",
   "metadata": {},
   "source": [
    "dict是用空间换时间的一种方法，dict的key必须是不可变对象，在python中字符串，整数都是不可变的，因此可以放心作为key。\n",
    "但是list是可变的，不能作为key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c27f64c8",
   "metadata": {},
   "source": [
    "### set"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28e33f06",
   "metadata": {},
   "source": [
    "set与dict类似，也是一组key集合，但不存储value，且key不重复。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "abb6cf59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 3, 4}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = set([4,2,3])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ce0cb2b",
   "metadata": {},
   "source": [
    "重复元素在set中自动被过滤："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "67f9a353",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = set([1,1,1,2,2,2,3,3,3])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57ae490f",
   "metadata": {},
   "source": [
    "通过add(key)方法可以添加元素到set中，可以重复添加，但不会有效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cd57a78b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 3, 4, 5}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.add(5)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8aa9cae",
   "metadata": {},
   "source": [
    "通过remove(key)方法可以删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9da9343d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3, 4, 5}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.remove(2)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8796088c",
   "metadata": {},
   "source": [
    "set可以看城市数学意义上无序和无重复元素的集合，因此可以做数学意义上的交集，并集等操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f4d01506",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({2, 3}, {1, 2, 3, 4})"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = set([1,2,3])\n",
    "s2 = set([2,3,4])\n",
    "s3 = s1 & s2 #交集\n",
    "s4 = s1 | s2 #并集\n",
    "s3,s4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6626b436",
   "metadata": {},
   "source": [
    "set和dict的唯一区别仅在于没有存储对应的value，因此set同样不能存放list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdb382d0",
   "metadata": {},
   "source": [
    "### 再议不可变对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "128e47f1",
   "metadata": {},
   "source": [
    "对于可变对象，操作之后，对象内容会发生变化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93e77cee",
   "metadata": {},
   "source": [
    "对于不可变对象，调用对象自身的任意方法，也不会改变对象自身内容。这些方法会创建新的对象并返回。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0327b81f",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c55f5544",
   "metadata": {},
   "source": [
    "tuple虽然是不可变对象，但是如果tuple中存在list，同样不能放在dict或set中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0a3f954",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
